package nodes.binary;

import parser.ParserException;
import model.Model;
import nodes.AbstractNode;
import nodes.VariableNode;


/**
 * Used for assigning values to variables
 * 
 * @author Andrew Patterson
 */
public class AssignNode extends AbstractBinaryNode
{

    public AssignNode (Model model)
    {
        super(model);
        myName = syntaxBundle.getString("equals_label");
    }


    public AbstractNode evaluate ()
    {
        // If argument is already a variable node
        if (myLeft instanceof VariableNode)
        {
            ((VariableNode) myLeft).setValue(myRight);
            return myRight.evaluate();
        }
        
        // If argument is another type of node it may evaluate 
        // to a variable node, thus we need to evaluate it
        AbstractNode tempNode = myLeft.evaluate();
        if(tempNode instanceof VariableNode)
        {
            ((VariableNode) tempNode).setValue(myRight);
            return myRight.evaluate();
        }

        throw ParserException.BAD_VARIABLE_ASSIGNMENT;

    }
}
